#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/5/26  15:55
# @Author: 余浪人
# @email: yulangren520@gmail.com


import requests, base64
from functools import wraps
from apps import setting
from flask import request, jsonify, make_response
from itsdangerous import TimedJSONWebSignatureSerializer
from apps.models.synthesize_model import TL_Connector_Model
from apps.models.system_model import TL_Validation_Model
def generate_token (OO000O0OOOO00O000 ,O0O00OOO0OOO0OO0O =setting .Config .EXPIRATION ):
    O0O0OO0O0O0O0OO0O =TimedJSONWebSignatureSerializer (setting .Config .SECRET_KEY ,expires_in =O0O00OOO0OOO0OO0O )
    return O0O0OO0O0O0O0OO0O .dumps ({'token':OO000O0OOOO00O000 }).decode ('ascii')
def apiVerify_token (O000O000OO0O00OOO ):
    @wraps (O000O000OO0O00OOO )
    def O0O000O0O0OOO00OO (*OOO0O00OOO0O0OOOO ,**OO0O0000O0OO0O0O0 ):
        O00O00O0000OO000O =request .json .get ('accessKey','')if request .is_json else request .form .get ('accessKey','')
        if not O00O00O0000OO000O :O00O00O0000OO000O =request.headers .get ('Accesskey','') if request.headers.get ('Accesskey',None) else request.args.get('accessKey','')
        if not O00O00O0000OO000O or len (O00O00O0000OO000O )!=32 :return jsonify ({"status":1 ,"msg":base64 .b64decode (b'6K+35aGr5YaZdG9rZW7lgLwh55So5LqO5p2D6ZmQ6aqM6K+B').decode ()})
        O00OOOOOOOO000000 =TL_Connector_Model .query .filter (TL_Connector_Model .impower_code ==O00O00O0000OO000O ).first ()
        if O00OOOOOOOO000000 is None :return jsonify ({'status':2 ,"msg":base64 .b64decode (b'5p2D6ZmQ6aqM6K+B5aSx6LSlLHRva2Vu5YC85peg5pWIIQ==').decode ()})
        if O00OOOOOOOO000000 .status ==0 :return jsonify ({'status':3 ,"msg":base64 .b64decode (b'5o6l5Y+j5pyq5byA5ZCvLOivt+WFiOW8gOWQr+S9v+eUqCE=').decode ()})
        OOOOO0O0OO0O0O00O =O000O000OO0O00OOO (*OOO0O00OOO0O0OOOO ,**OO0O0000O0OO0O0O0 )
        return OOOOO0O0OO0O0O00O
    return O0O000O0O0OOO00OO
def Verify_authorization (OO0OOOOO0000OO00O ):
    @wraps (OO0OOOOO0000OO00O )
    def OOOO00O0O00OO00OO (*O0O000O00OOOO00OO ,**O00OO00O0OO00O0OO ):
        OO0O00O0OO000OO00 =request .host_url
        OO0O0O0O000OOOO00 =TL_Validation_Model .query .order_by (TL_Validation_Model .id .desc ()).filter_by ().first ()
        OO0O00OO0O000OO0O =base64 .b64decode (b'PGRpdiBzdHlsZT0icG9zaXRpb246IGFic29sdXRlO3dpZHRoOiAxMDAlO2hlaWdodDogMTAwJTsiPjxoMiBzdHlsZT0idGV4dC1hbGlnbjogY2VudGVyO21hcmdpbi10b3A6IDIwJTsiPuiuv+mXruWPl+mZkCzor7fojrflj5bns7vnu5/mjojmnYM8L2gyPjxkaXY+').decode ()
        OOO000O000OO0O000 =make_response (OO0O00OO0O000OO0O )
        OOO000O000OO0O000 .headers ['Content-Type']='text/html; charset=utf-8'
        if not OO0O0O0O000OOOO00 :return OOO000O000OO0O000
        try :
            _O0000OOO0O00OO000 =requests .post (base64 .b64decode (b'aHR0cDovL3d3dy50bGNtcy5uZXQvYXV0aGVudGljYXRpb24=').decode (),data ={'domain':OO0O00O0OO000OO00 ,'auth_code':OO0O0O0O000OOOO00 .v_code })
            if _O0000OOO0O00OO000 .json ()['status']!=0 :
                return OOO000O000OO0O000
        except :
            return OOO000O000OO0O000
        OO0OO0O000O0O0OOO =OO0OOOOO0000OO00O (*O0O000O00OOOO00OO ,**O00OO00O0OO00O0OO )
        return OO0OO0O000O0O0OOO
    return OOOO00O0O00OO00OO
